package com.collectplus.express.parcel;

import java.util.ArrayList;
import java.util.HashSet;

import android.app.Activity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.collectplus.express.R;
import com.collectplus.express.model.ParcelDetailBean;

import droid.frame.activity.base.BaseAdapterWithImage;

/**
 * 驿站扫码 核对包裹
 *
 * @author coffee<br>
 *         2016-12-5下午9:47:40
 */
public class ParcelCheckAdapter extends BaseAdapterWithImage<ParcelDetailBean> {

    /**
     * 选中的item的id
     */
    private HashSet<Integer> checkedIds;

    public ParcelCheckAdapter(ArrayList<ParcelDetailBean> items, Activity context, ViewGroup listView) {
        super(items, context, listView);
        checkedIds = new HashSet<Integer>();
        // 注意，别忘记加super ,super.items中已经做非空判断了, items是传入的参数
        super.items.clear();
        // for (int i = 0; i < 6; i++) {
        // ParcelDetailBean item = new ParcelDetailBean();
        // item.setId(i + 1);
        // items.add(item);
        // // 默认全部选中
        // checkedIds.add(item.getId());
        // }
        setEmptyView(R.layout.parcel_check_empty);
    }

    /**
     * 如果当前item已经选中 , 则取消选中
     *
     * @param id
     */
    public void setChecked(int id) {
        if (checkedIds.contains(id)) {
            checkedIds.remove(id);
        } else {
            checkedIds.add(id);
        }
        notifyDataSetChanged();
    }

    /**
     * 选中|取消全部
     */
    public void checkAlls(boolean isChecked) {
        if (items == null) {
            return;
        }
        if (isChecked == false) {
            checkedIds.clear();
        } else {
            for (int i = 0; i < items.size(); i++) {
                ParcelDetailBean item = getItem(i);
                checkedIds.add(item.getId());
            }
        }
        // update
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (hasEmptyView()) {
            return super.getView(position, convertView, parent);
        }
        ViewHolder holder = null;
        if (convertView == null || convertView.getTag() == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.parcel_check_item, parent, false);
            holder.goodsImg = (ImageView) convertView.findViewById(R.id.parcel_goods_image);
            holder.bagCode = (TextView) convertView.findViewById(R.id.parcel_bagCode);
            holder.receiverName = (TextView) convertView.findViewById(R.id.parcel_receiver_name);
            holder.receiverPhone = (TextView) convertView.findViewById(R.id.parcel_receiver_phone);
            holder.goodsType = (TextView) convertView.findViewById(R.id.parcel_goods_type);
            holder.checked = (CheckBox) convertView.findViewById(R.id.parcel_checkbox);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ParcelDetailBean item = getItem(position);
        // AppLogic.getInstance().loadImage(item.getParcelImgUrl(), holder.goodsImg);
        showImage(item.getParcelImgUrl(), holder.goodsImg, null);
        if (!TextUtils.isEmpty(item.getBagCode()) && !"null".equals(item.getBagCode())) {
            holder.bagCode.setText("投递编号: " + item.getBagCode());
        }else{
            holder.bagCode.setText("投递编号: " + context.getString(R.string.parcel_nobag_code));
        }
        holder.receiverName.setText("收件人: " + item.getReceiverName());
        holder.receiverPhone.setText("电话: " + item.getReceiverPhone());
        holder.goodsType.setText("物品类型: " + item.getCategory());
        if (checkedIds.contains(item.getId())) {
            holder.checked.setChecked(true);
        } else {
            holder.checked.setChecked(false);
        }
        return convertView;
    }

    private class ViewHolder {
        ImageView goodsImg;
        TextView bagCode;
        TextView receiverName;
        TextView receiverPhone;
        TextView goodsType; // 物品类型
        CheckBox checked;
    }

    public HashSet<Integer> getCheckIds() {
        if (checkedIds == null) {
            checkedIds = new HashSet<Integer>();
        }
        return checkedIds;
    }

}
